package Login;

import java.util.Random;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;


public class VerifyCode {
        /**
	 * Verify Code Type just Integer 0~9
	 */
	public static final int TYPE_NUM_ONLY=0;

        /**
	 * Verify Code Type just letter(mix Upper and Lower Case)
	 */
	public static final int TYPE_LETTER_ONLY=1;
        
        /**
	 * Verify Code Type  Integer and Letter(mix Upper and Lower Case)
	 */
	public static final int TYPE_ALL_MIXED=2;

        /**
	 * Verify Code Type  Integer and Letter(Upper Case)
	 */
	public static final int TYPE_NUM_UPPER=3;		

        /**
	 * Verify Code Type  Integer and Letter(Lower Case)
	 */
	public static final int TYPE_NUM_LOWER=4;	
	
        /**
	 * Verify Code Type just Letter(Upper)
	 */
	public static final int TYPE_UPPER_ONLY=5;

        /**
	 * Verify Code Type Letter(Lower Case)
	 */
	public static final int TYPE_LOWER_ONLY=6;
	
		
	VerifyCode(){}

        /**
	 * generate String type of verifyCode
	 * @param type type of verifyCode，please read the static attribute
	 * @param length lenght of verifyCOde，greater than 0;
	 * @param exChars exclude the special char (only for the type of mix integer and letter,
         *  if input null, will not to exclude the special char）
	 * @return verifyCode string
	 */
	public static String generateTextCode(int type,int length,String exChars){
		
		if(length<=0) return "";
		
		StringBuffer code=new StringBuffer();
		int i=0;
		Random r=new Random();
		
		switch(type)
		{
		
		//just Integer
		case TYPE_NUM_ONLY:
			while(i<length){
	            int t=r.nextInt(10);
				if(exChars==null||exChars.indexOf(t+"")<0){
					code.append(t);
					i++;
				}
			}
			break;
			
                //just letter(mix Upper and Lower Case)
		case TYPE_LETTER_ONLY:
			while(i<length){
				int t=r.nextInt(123);
				if((t>=97||(t>=65&&t<=90))&&(exChars==null||exChars.indexOf((char)t)<0)){
					code.append((char)t);
					i++;	
				}
			}
			break;
		
		// mix Integer and letter(Upper and Lower Case)���
		case TYPE_ALL_MIXED:
			while(i<length){
				int t=r.nextInt(123);
				if((t>=97||(t>=65&&t<=90)||(t>=48&&t<=57))&&(exChars==null||exChars.indexOf((char)t)<0)){
					code.append((char)t);
					i++;
				}
			}
			break;	
			
		//Integer and letter(Upper Case)�
		case TYPE_NUM_UPPER:
			while(i<length){
				int t=r.nextInt(91);
				if((t>=65||(t>=48&&t<=57))&&(exChars==null||exChars.indexOf((char)t)<0)){
					code.append((char)t);
					i++;
				}
			}
			break;		
			
		//Integer and letter(Lower Case)�
		case TYPE_NUM_LOWER:
			while(i<length){
				int t=r.nextInt(123);
				if((t>=97||(t>=48&&t<=57))&&(exChars==null||exChars.indexOf((char)t)<0)){
					code.append((char)t);
					i++;
				}
			}
		break;		
			
		//only Letter(Upper)�
		case TYPE_UPPER_ONLY:
			while(i<length){
				int t=r.nextInt(91);
				if((t>=65)&&(exChars==null||exChars.indexOf((char)t)<0)){
					code.append((char)t);
					i++;
				}
			}
		break;				
			
		//only Letter(Lower Case)�
		case TYPE_LOWER_ONLY:
			while(i<length){
				int t=r.nextInt(123);
				if((t>=97)&&(exChars==null||exChars.indexOf((char)t)<0)){
					code.append((char)t);
					i++;
				}
			}
		break;				
		
		}
		
		return code.toString();
	}
        

        /**
	 * there have verify code, generate verify code image
	 * @param textCode String of verifyy code
	 * @param width width of image
	 * @param height height of image
	 * @param interLine how many inter-line in the image
	 * @param randomLocation is the random Loacation each char
	 * @param backColor image color, if null, random color
	 * @param foreColor font color, if null, random color
	 * @param lineColor inter-line color, if null, random color
	 * @return cache of image
	 */
	public static BufferedImage generateImageCode(String textCode,int width,
                int height,int interLine,boolean randomLocation,
                Color backColor,Color foreColor,Color lineColor){
		
		
		BufferedImage bim=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
		Graphics g=bim.getGraphics();
		
		// draw background  
		g.setColor(backColor==null?getRandomColor():backColor);
		g.fillRect(0,0,width,height);
		
		// draw inter-line
		Random r=new Random();
		if(interLine>0){
	
			int x=0,y=0,x1=width,y1=0;
			for(int i=0;i<interLine;i++){
				g.setColor(lineColor==null?getRandomColor():lineColor);
				y=r.nextInt(height);
				y1=r.nextInt(height);
	
				g.drawLine(x,y,x1,y1);
			}
		}
                
                //generate verify code
                
                // set size of font which is 80% height of image
		int fsize=(int)(height*0.8);
		int fx=height-fsize;
		int fy=fsize;

		g.setFont(new Font(Font.DIALOG,Font.PLAIN,fsize));
		
                // write verify code into image
		for(int i=0;i<textCode.length();i++){
			fy=randomLocation?(int)((Math.random()*0.3+0.6)*height):fy;
			g.setColor(foreColor==null?getRandomColor():foreColor);
			g.drawString(textCode.charAt(i)+"",fx,fy);
			fx+=fsize*0.85;
		}
	
		g.dispose();
		
		return bim;
	}
        
	private static Color getRandomColor(){
		Random r=new Random();
		Color c=new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
		return c;
	}

}
